package com.crazymakercircle.imServer.distributed;

import lombok.Data;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * IM节点的POJO类
 * create by 尼恩 @ 疯狂创客圈
 **/
@Data
public class ImNode implements Comparable<ImNode> {

    //worker 的Id,zookeeper负责生成
    private long id;

    //Netty 服务 的连接数
    private AtomicInteger balance;

    //Netty 服务 IP
    private String host;

    //Netty 服务 端口
    private String port;

    public ImNode(String host, String port) {
        this.host = host;
        this.port = port;
    }

    public static ImNode getLocalInstance() {
        return null;
    }

    @Override
    public String toString() {
        return "ImNode{" +
                "id='" + id + '\'' +
                "host='" + host + '\'' +
                ", port='" + port + '\'' +
                ",balance=" + balance +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImNode node = (ImNode) o;
        return id == node.id &&
                Objects.equals(host, node.host) &&
                Objects.equals(port, node.port);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, host, port);
    }

    /**
     * 升序排列
     */
    public int compareTo(ImNode o) {
        int weight1 = this.getBalance().get();
        int weight2 = o.getBalance().get();
        if (weight1 > weight2) {
            return 1;
        } else if (weight1 < weight2) {
            return -1;
        }
        return 0;
    }


}